package com.qys.livesMall.event.listener;

import cn.hutool.core.bean.BeanUtil;
import com.qys.livesMall.app.user.event.UserShopStatisticsEvent;
import com.qys.livesMall.app.user.mapper.UserShopStatisticsMapper;
import com.qys.livesMall.app.user.model.dto.sys.UserStatisticsInfoDTO;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.app.user.model.entity.UserShopStatistics;
import com.qys.livesMall.app.user.model.entity.UserStatisticsTeam;
import com.qys.livesMall.app.user.model.entity.UserStatisticsInfo;
import com.qys.livesMall.app.user.service.*;
import com.qys.livesMall.app.user.utils.IpLocation;
import com.qys.livesMall.app.user.utils.IpUtils;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户注册监听
 */
@Slf4j
@RequiredArgsConstructor
public class UserShopStatisticsListener {
    private final UserShopStatisticsService userShopStatisticsService;
    private final UserStatisticsInfoService userStatisticsInfoService;
    private final UserStatisticsTeamService userStatisticsTeamService;
    private final IpOperateStatisticsService ipOperateStatisticsService;
    private final UserShopStatisticsMapper userShopStatisticsMapper;
    private final IH2AppUserService appUserService;

    @Order
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT,classes = UserShopStatisticsEvent.class)
    public void saveUserStatisticsInfo(UserShopStatisticsEvent userShopStatisticsEvent){
        log.info("===========用户注册监听===========");
        //获取数据
        UserStatisticsInfoDTO userStatisticsInfoDTO= (UserStatisticsInfoDTO) userShopStatisticsEvent.getSource();
        if (!userStatisticsInfoDTO.getVirtualUsers().equals("0") && !userStatisticsInfoDTO.getVirtualUsers().equals("1")){
            return;
        }
        //保存共同数据
        UserShopStatistics userShopStatistics=new UserShopStatistics();
        BeanUtil.copyProperties(userStatisticsInfoDTO,userShopStatistics);
        IpLocation ipLocation= IpUtils.getLocation(userShopStatistics.getRegisterIp());
        userShopStatistics.setCountry(ipLocation.getCountry());
        //店铺和会员通用统计表
        UserShopStatistics userShop=userShopStatisticsService.getbyUserName(userStatisticsInfoDTO.getSuperior());
        userShopStatistics.setAgentAccount(userShop.getAgentAccount());
        userShopStatistics.setTeamId(userShop.getTeamId());
        if (userStatisticsInfoDTO.getVirtualUsers().equals("1")){
            List<UserStatisticsTeam> userStatisticsTeams = userStatisticsTeamService.getByTeamId(userShop.getTeamId());
            UserStatisticsTeam userStatisticsTeam = userStatisticsTeams.get(0);
            userStatisticsTeam.setRegisterNum(userStatisticsTeam.getRegisterNum()+1);
            userStatisticsTeamService.updateById(userStatisticsTeam);
        }
        if (null != userShop){
            userShopStatistics.setStorey(userShop.getStorey()+1);
        }else {
            userShopStatistics.setStorey(1);
        }
        userShopStatisticsService.save(userShopStatistics);
        //用户信息保存
        UserStatisticsInfo userStatisticsInfo=new UserStatisticsInfo();
        BeanUtil.copyProperties(userStatisticsInfoDTO,userStatisticsInfo);
        //更新上3级数据
        if (null != userShopStatistics.getSuperior()){
            String superior=userShopStatistics.getSuperior();
            for (int i = 0; i < 3; i++) {
                UserShopStatistics  oneUser = userShopStatisticsMapper.getbyUserName(superior);
                UserStatisticsInfo oneUserStatistics=userStatisticsInfoService.selectByUserId(oneUser.getUserId());
                if (null==userStatisticsInfo.getRegisterNum()){
                    H2AppUser h2AppUser=appUserService.getById(oneUser.getUserId());
                    if (h2AppUser.getUserSysType() == 1 || h2AppUser.getUserSysType() == 2 ){
                        userStatisticsInfo.setRegisterNum(1);
                    }else {
                        userStatisticsInfo.setRegisterNum(0);
                    }
                }
                oneUserStatistics.setRegisterNum(userStatisticsInfo.getRegisterNum()+1);
                userStatisticsInfoService.updateById(oneUserStatistics);
                if (null==oneUser.getSuperior()){
                    break;
                }
                superior=oneUser.getSuperior();
            }
        }
        userStatisticsInfoService.save(userStatisticsInfo);
        ipOperateStatisticsService.add(userStatisticsInfoDTO.getRegisterIp(),0);
    }
}
